package com.express.wechat.controller;

import com.aliyuncs.unmarshaller.JsonUnmashaller;
import com.express.bean.Courier;
import com.express.bean.Message;
import com.express.bean.User;
import com.express.bean.UserType;
import com.express.mvc.ResponseBody;
import com.express.service.CourierService;
import com.express.service.UserService;
import com.express.util.JSONUtil;
import com.express.util.RandomUtil;
import com.express.util.SMSUtil;
import com.express.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: laixiangdong
 * @Date 2020/12/09
 */
public class WechatUserController {
    @ResponseBody("/loginSms.do")
    public String sendSms(HttpServletRequest req, HttpServletResponse resp) {
        String userPhone = req.getParameter("userPhone");
        String code = RandomUtil.getCode();
        System.out.println("发短信：" + userPhone + "  " + code);
        boolean flag = true;
        // boolean flag = SMSUtil.sendLoginCode(userPhone, code);
        Message message = new Message();
        if (flag) {
            // 发送成功
            message.setStatus(0);
            message.setResult("验证码发送成功，请查收");
            UserUtil.setWechatUserLoginCode(req.getSession(), userPhone, code);
        } else {
            // 发送失败
            message.setStatus(-1);
            message.setResult("发送失败，请检查手机号，或稍后再试");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/sendSms.do")
    public String sendSms2(HttpServletRequest req, HttpServletResponse resp) {
        String userPhone = req.getParameter("userPhone");
        String code = RandomUtil.getCode();
        System.out.println("发短信：" + userPhone + "  " + code);
        boolean flag = true;
        // boolean flag = SMSUtil.sendLoginCode(userPhone, code);
        Message message = new Message();
        if (flag) {
            // 发送成功
            message.setStatus(0);
            message.setResult("验证码发送成功，请查收");
            UserUtil.setWechatUserCode(req.getSession(), code);
        } else {
            // 发送失败
            message.setStatus(-1);
            message.setResult("发送失败，请稍后再试");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp) {
        String userPhone = req.getParameter("userPhone");
        String userCode = req.getParameter("code");
        String sysCode = UserUtil.getWechatUserLoginCode(req.getSession(), userPhone);
        Message message = new Message();
        if (sysCode == null) {
            // 未获取手机验证码
            message.setStatus(-1);
            message.setResult("还没有获取手机验证码");
        } else if (sysCode.equals(userCode)) {

            // 手机验证码一致，登陆成功
            Courier courier = CourierService.findByPhone(userPhone);
            User user = UserService.findByPhone(userPhone);
            if (courier != null) {
                // 表示这个手机号是快递员的
                message.setStatus(1);
                UserUtil.setWechatUserType(req.getSession(), UserType.COURIER);
            } else if (user != null) {
                // 用户
                message.setStatus(0);
                UserUtil.setWechatUserType(req.getSession(), UserType.USER);
            } else {
                // 注册
                message.setStatus(2);
                UserUtil.setWechatUserType(req.getSession(), UserType.USER);
                User user1 = new User("user_" + userPhone, "123456", userPhone, "未认证");
                UserService.insert(user1);
            }
            UserUtil.setWechatUserPhone(req.getSession(), userPhone);
        } else {
            message.setStatus(-2);
            message.setResult("验证码不一致");
            // 手机验证码不一致
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/logout.do")
    public String logout(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        session.invalidate();
        Message message = new Message(0);
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/userType.do")
    public String userType(HttpServletRequest req, HttpServletResponse resp) {
        UserType type = UserUtil.getWechatUserType(req.getSession());
        Message message = new Message();
        if (type == UserType.COURIER) {
            message.setStatus(1);
        } else {
            message.setStatus(0);
        }
        message.setResult(UserUtil.getUserPhone(req.getSession()));
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/user.do")
    public String admin(HttpServletRequest req, HttpServletResponse resp) {
        Message message = null;
        HttpSession session = req.getSession();
        String userPhone = UserUtil.getWechatUserPhone(session);
        UserType type = UserUtil.getWechatUserType(session);
        if (type == UserType.COURIER) {
            Courier courier = CourierService.findByPhone(userPhone);
            message = new Message(0, "查询成功", courier);
        } else if (type == UserType.USER) {
            User user = UserService.findByPhone(userPhone);
            message = new Message(0, "查询成功", user);
        } else {
            message = new Message(-1, "查询失败", null);
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/update.do")
    public String updateUser(HttpServletRequest req, HttpServletResponse resp) {
        Message message = null;
        String userPhone = req.getParameter("userPhone");
        String newUserPhone = req.getParameter("newUserPhone");
        String code = req.getParameter("code");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String cardId = req.getParameter("cardId");
        UserType type = UserUtil.getWechatUserType(req.getSession());
        String userCode = UserUtil.getWechatUserCode(req.getSession());
        if (userCode != null && userCode.equals(code)) {
            if (type == UserType.COURIER) {
                int id = CourierService.findByPhone(userPhone).getId();
                Courier courier = new Courier(userName, password, cardId, newUserPhone);
                Boolean flag = CourierService.update(id,courier);
                if (flag)
                    message = new Message(0,"保存成功");
                else
                    message = new Message(-2,"保存失败");
            } else if (type == UserType.USER) {
                int id = UserService.findByPhone(userPhone).getId();
                User user = new User(userName, password, userPhone, cardId);
                Boolean flag = UserService.update(id,user);
                if (flag)
                    message = new Message(0,"保存成功");
                else
                    message = new Message(-2,"保存失败");
            }
        }else {
            message = new Message(-1,"状态码不正确");
        }
        return JSONUtil.toJSON(message);
    }
}
